//
//  ExplainManager.m
//  Grammar
//
//  Created by Rick on 7/14/13.
//
//

#import "ExplainManager.h"
#import "GrammarExplain.h"
#import "ITTObjectSingleton.h"

#define TYGrammarExplain @"TYGrammarExplain"
#define TYGrammarExplainArchiver @"TYGrammarExplainArchiver"

@interface ExplainManager()
- (void)restore;
@end

@implementation ExplainManager
static ExplainManager *_sharedInst = nil;

+(ExplainManager*)sharedManager
{
    @synchronized(self) {
        if (_sharedInst == nil) {
            _sharedInst = [[self alloc] init];
        }
    }
    return _sharedInst;
}

- (id)init
{
    if ((self = [super init])) {
        [self restore];
    }
    return self;
}

- (void)saveData
{
    if (_grammarExplains) {
        NSMutableData *data = [[NSMutableData alloc] init];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
                                     initForWritingWithMutableData:data];
        [archiver encodeObject:_grammarExplains forKey:TYGrammarExplainArchiver];
        [archiver finishEncoding];
        [USER_DEFAULT setObject:data forKey:[NSString stringWithFormat:@"%@%@",TYGrammarExplain,_grammarId] ];
        [archiver release];
        [data release];
        
        [USER_DEFAULT synchronize];
    }
}

- (void)readData
{
    NSData *data = [USER_DEFAULT objectForKey:[NSString stringWithFormat:@"%@%@",TYGrammarExplain,_grammarId]];;
    if (!data) {
        return;
    }
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    NSArray *array = [unarchiver decodeObjectForKey:TYGrammarExplainArchiver];
    [unarchiver finishDecoding];
    [_grammarExplains addObjectsFromArray:array];
    [unarchiver release];
}

- (void)addGrammarExplain:(GrammarExplain *)grammarExplain
{
    for (GrammarExplain *aGrammarExplain in self.grammarExplains) {
        if ([aGrammarExplain.explainId isEqualToString:grammarExplain.explainId]) {
            return;
        }
    }
    [_grammarExplains insertObject:grammarExplain atIndex:0];
    
    [self saveData];
}

- (void)updateGrammarExplain:(GrammarExplain *)grammarExplain
{
    NSArray *array = [NSArray arrayWithArray:self.grammarExplains];
    for (GrammarExplain *aGrammarExplain in array) {
        if ([aGrammarExplain.explainId isEqualToString:grammarExplain.explainId]) {
            [_grammarExplains removeObject:aGrammarExplain];
            [self saveData];
        }
    }
    
    [_grammarExplains insertObject:grammarExplain atIndex:0];
    
    [self saveData];
}

- (void)removeGrammarExplain:(GrammarExplain *)grammarExplain
{
    NSArray *array = [NSArray arrayWithArray:self.grammarExplains];
    for (GrammarExplain *aGrammarExplain in array) {
        if ([aGrammarExplain.explainId isEqualToString:grammarExplain.explainId]) {
            [_grammarExplains removeObject:aGrammarExplain];
            [self saveData];
        }
    }
}

- (NSMutableArray*)grammarExplains
{
    [self clearMemoryData];
    [self readData];

    return _grammarExplains;
}

- (void)restore
{
    _grammarExplains = [[NSMutableArray alloc] init];
    [self readData];
}
- (void)dealloc
{
    [_grammarExplains release];
    [super dealloc];
}
- (void)clearMemoryData
{
    [_grammarExplains removeAllObjects];
}

- (void)removeAllGrammarExplains
{
    [_grammarExplains removeAllObjects];
    [USER_DEFAULT removeObjectForKey:[NSString stringWithFormat:@"%@%@",TYGrammarExplain,_grammarId]];
    [USER_DEFAULT synchronize];
}
@end
